package com.sdp.wx.db.wx_repair_vehicle_item.service.impl;

import com.sdp.wx.db.wx_repair_vehicle.entity.WxRepairVehicle;
import com.sdp.wx.db.wx_repair_vehicle_item.entity.WxRepairVehicleItem;
import com.sdp.wx.db.wx_repair_vehicle_item.mapper.WxRepairVehicleItemMapper;
import com.sdp.wx.db.wx_repair_vehicle_item.service.IWxRepairVehicleItemService;
import com.sdp.wx.view.WxRepairMustItemView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-01-18
 */
@Service
public class WxRepairVehicleItemServiceImpl extends ServiceImpl<WxRepairVehicleItemMapper, WxRepairVehicleItem> implements IWxRepairVehicleItemService {

	@Override
	public List<WxRepairVehicleItem> queryWxRepairVehicleItemByRepairCode(String repairCode) {
		LambdaQueryWrapper<WxRepairVehicleItem> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(WxRepairVehicleItem::getRepairCode, repairCode);
		return list(queryWrapper);
	}

	@Override
	public void addMustRepairVehicleItem(List<WxRepairMustItemView> mustItemList, WxRepairVehicle repairVehicle) {
		List<WxRepairVehicleItem> items = new ArrayList<>();
		for(WxRepairMustItemView v : mustItemList) {
			WxRepairVehicleItem item = new WxRepairVehicleItem();
			item.setItemNo(v.getItemNo());
			item.setItemName(v.getItemName());
			item.setItemType(v.getItemType());
			item.setItemHour(v.getItemHour());
			item.setRepairCode(repairVehicle.getRepairCode());
			item.setHourFactor(repairVehicle.getHourFactor());
			item.setHourQuota(repairVehicle.getHourQuota());
			item.setHoutAmount(item.getItemHour().multiply(repairVehicle.getHourQuota()).multiply(repairVehicle.getHourFactor()).setScale(2,BigDecimal.ROUND_HALF_UP));
			items.add(item);
		}
		saveBatch(items);
		
	}

	@Override
	public boolean isDispatch(String repairCode) {
		List<WxRepairVehicleItem> itemList = queryWxRepairVehicleItemByRepairCode(repairCode);
		if(itemList==null || itemList.size()==0) {
			return true;
		}
		boolean flag = true;
		for(WxRepairVehicleItem item : itemList) {
			if(StringUtils.isEmpty(item.getRepairWorker())) {
				flag = false;
				break;
			}
		}
		return flag;
	}

	@Override
	public BigDecimal queryHourAmount(String repairCode) {
		LambdaQueryWrapper<WxRepairVehicleItem> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(WxRepairVehicleItem::getRepairCode, repairCode);
		List<WxRepairVehicleItem> list = list(queryWrapper);
		if(list==null || list.size()==0) {
			return BigDecimal.ZERO;
		}
		BigDecimal hourAmount = BigDecimal.ZERO;
		for(WxRepairVehicleItem item : list) {
			hourAmount = hourAmount.add(item.getHoutAmount());
		}
		
		return hourAmount;
	}

}
